package com.lxh.singleton;

import java.util.ArrayList;
import java.util.Random;

/**
 * 多例模式
 * Create by lxh on 2020/2/13
 */
public class ManyEmperor {

    // 最多两个皇帝
    private static int maxNum = 2;

    private static ArrayList<ManyEmperor> emperors = new ArrayList<>(maxNum);

    // 目前是哪个皇帝
    private int countNum = 0;

    private ManyEmperor() {}

    static {
        for (int i = 0; i < maxNum; i++) {
            emperors.add(new ManyEmperor());
        }
    }

    public static ManyEmperor getInstance() {
        Random random = new Random();
        int current = random.nextInt(maxNum);
        return emperors.get(current);
    }

    public static ManyEmperor getInstance(int index) {
        return emperors.get(index);
    }
}
